package com.kanche.bigbaby.collection.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.StringUtils;
import com.kanche.bigbaby.collection.domain.Photo;
import com.kanche.bigbaby.support.awesome.ButtonAwesome;
import com.kanche.bigbaby.support.awesome.DrawableAwesome;

//import com.kkche.bss.R;
//import com.kkche.bss.awesome.ButtonAwesome;
//import com.kkche.bss.awesome.DrawableAwesome;
//import com.kkche.bss.dialogs.KancheDialogFragment;
//import com.kkche.bss.domain.Photo;
//import com.kkche.bss.utils.StringUtils;
//import com.kkche.merchant.R;
//import com.kkche.merchant.awesome.ButtonAwesome;
//import com.kkche.merchant.awesome.DrawableAwesome;
//import com.kkche.merchant.domain.Photo;
//import com.kkche.merchant.utils.StringUtil;

/**
 * Created by nealmi on 6/10/14.
 */
public class VehiclePhotoSelectionDialog extends KancheDialogFragment implements View.OnClickListener {


    private Photo photo;
    private ButtonAwesome selectButton;
    private ButtonAwesome captureButton;
    private ButtonAwesome deleteButton;
    private OnChoiceMadeListener listener;
    private ButtonAwesome setButton;
    private ButtonAwesome editButton;

    public VehiclePhotoSelectionDialog() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_photo_btn:
                if (listener != null) {
                    listener.onDelete(photo);
                }
                break;
            case R.id.capture_photo_btn:
                if (listener != null) {
                    listener.onCameraSelected();
                }
                break;
            case R.id.select_photo_btn:
                if (listener != null) {
                    listener.onMediaStoreSelected();
                }
                break;
            case R.id.set_cover_btn:
                if (listener != null) {
                    listener.onSetCover(photo);
                }
                break;
            case R.id.edit_photo_btn:
                if (listener != null) {
                    listener.onEditImage(photo);
                }
                break;
        }

    }


    public Photo getPhoto() {
        return photo;
    }

    public void setPhoto(Photo photo) {
        this.photo = photo;
    }

    public OnChoiceMadeListener getListener() {
        return listener;
    }

    public void setListener(OnChoiceMadeListener listener) {
        this.listener = listener;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_photo_selection_collect, container);
        captureButton = (ButtonAwesome) view.findViewById(R.id.capture_photo_btn);
        selectButton = (ButtonAwesome) view.findViewById(R.id.select_photo_btn);
        setButton = (ButtonAwesome) view.findViewById(R.id.set_cover_btn);
        deleteButton = (ButtonAwesome) view.findViewById(R.id.delete_photo_btn);
        editButton = (ButtonAwesome) view.findViewById(R.id.edit_photo_btn);
        

        int color = getResources().getColor(R.color.kkche_gray);
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics());

        Context context = getActivity();
        Drawable d1 = new DrawableAwesome.DrawableAwesomeBuilder(context, R.string.fa_camera).setColor(color).setSize(size).build();
        Drawable d2 = new DrawableAwesome.DrawableAwesomeBuilder(context, R.string.fa_picture_o).setColor(color).setSize(size).build();
        Drawable d3 = new DrawableAwesome.DrawableAwesomeBuilder(context, R.string.fa_bookmark).setColor(color).setSize(size).build();
        Drawable d4 = new DrawableAwesome.DrawableAwesomeBuilder(context, R.string.fa_trash_o).setColor(color).setSize(size).build();
        Drawable d5 = new DrawableAwesome.DrawableAwesomeBuilder(context, R.string.fa_pencil_square_o).setColor(color).setSize(size).build();
        captureButton.setCompoundDrawablesWithIntrinsicBounds(null, d1, null, null);
        selectButton.setCompoundDrawablesWithIntrinsicBounds(null, d2, null, null);
        setButton.setCompoundDrawablesWithIntrinsicBounds(null, d3, null, null);
        deleteButton.setCompoundDrawablesWithIntrinsicBounds(null, d4, null, null);
        editButton.setCompoundDrawablesWithIntrinsicBounds(null, d5, null, null);

        if (photo != null && StringUtils.hasText(photo.getUri())) {
            deleteButton.setVisibility(View.VISIBLE);
            setButton.setVisibility(View.VISIBLE);
            editButton.setVisibility(View.VISIBLE);
            captureButton.setVisibility(View.GONE);
            selectButton.setVisibility(View.GONE);
            if(StringUtils.isHttpPath(photo.getUri()))
                editButton.setVisibility(View.GONE);
        } else {
            deleteButton.setVisibility(View.GONE);
            setButton.setVisibility(View.GONE);
            editButton.setVisibility(View.GONE);
            captureButton.setVisibility(View.VISIBLE);
            selectButton.setVisibility(View.VISIBLE);
        }
        captureButton.setOnClickListener(this);
        selectButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
        setButton.setOnClickListener(this);
        editButton.setOnClickListener(this);
        getDialog().setTitle("提示");

        return view;
    }

    public interface OnChoiceMadeListener {
        void onCameraSelected();

        void onMediaStoreSelected();

        void onDelete(Photo photo);

        void onSetCover(Photo photo);

        void onEditImage(Photo photo);
    }

}